package pin.db.schema.base;
import pin.db.schema.*;
import java.util.Vector;
import java.util.Collection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class UploadPeer extends BasePeer{
	public static final String tableName = "Upload";
	public static final String UPLOADID = "uploadId";
	public static final String USERID = "userId";
	public static final String NAME = "name";
	public static final String ORIGINFILENAME = "originFilename";
	public static final String EXTENTION = "extention";
	public static final String FILETYPE = "filetype";
	public static final String SIZE = "size";
	public static final String UPLOADDATE = "uploadDate";
	public static final String DOWNLOADS = "downloads";
	public static final String LASTDOWNLOAD = "lastDownload";
	public static final String PUBLICPOLICY = "publicPolicy";
	public static final String PHYSICALFILENAME = "physicalFilename";
	public static final String PHYSICALPATH = "physicalPath";
	public static final String PHYSICALACCESS = "physicalAccess";
	public static final String STATE = "state";
	public static final String COMMENT = "comment";
	public static final String CATEGORIES = "categories";
	public static final String[] columnNames=new String[]{"uploadId","userId","name","originFilename","extention","filetype","size","uploadDate","downloads","lastDownload","publicPolicy","physicalFilename","physicalPath","physicalAccess","state","comment","categories"};
	public static Upload getUploadByPKs(Object uploadId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UploadPeer.tableName,UploadPeer.columnNames," TRUE  AND `uploadId`=? ",new Object[]{uploadId,},0, 1,conn);
			Upload upload = new Upload();
		fill(rs,upload);
		return upload;
	}
	public static Upload getUploadReferenceColumnsOnlyByPKs(Object uploadId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UploadPeer.tableName,new String[]{"uploadId",}," TRUE AND `uploadId`=?",new Object[]{uploadId,}, 0,  1,conn);
			Upload upload = new Upload();
		fill(rs,upload);
		return upload;
	}
	public static Upload getUploadByUploadId(long uploadId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UploadPeer.tableName,UploadPeer.columnNames,"`uploadId`=?",new Object[]{uploadId},0, 1,conn);
			Upload upload = new Upload();
		fill(rs,upload);
		return upload;
	}
	public static void fill(ResultSet rs, Upload upload) throws SQLException {
		if(rs.next()){
			try{upload.setUploadId(rs.getLong("uploadId"));}catch(Exception ex){}
			try{upload.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			try{upload.setName(rs.getString("name"));}catch(Exception ex){}
			try{upload.setOriginFilename(rs.getString("originFilename"));}catch(Exception ex){}
			try{upload.setExtention(rs.getString("extention"));}catch(Exception ex){}
			try{upload.setFiletype(rs.getString("filetype"));}catch(Exception ex){}
			try{upload.setSize(rs.getLong("size"));}catch(Exception ex){}
			try{upload.setUploadDate(rs.getTimestamp("uploadDate"));}catch(Exception ex){}
			try{upload.setDownloads(rs.getLong("downloads"));}catch(Exception ex){}
			try{upload.setLastDownload(rs.getTimestamp("lastDownload"));}catch(Exception ex){}
			try{upload.setPublicPolicy(rs.getInt("publicPolicy"));}catch(Exception ex){}
			try{upload.setPhysicalFilename(rs.getString("physicalFilename"));}catch(Exception ex){}
			try{upload.setPhysicalPath(rs.getString("physicalPath"));}catch(Exception ex){}
			try{upload.setPhysicalAccess(rs.getString("physicalAccess"));}catch(Exception ex){}
			try{upload.setState(rs.getInt("state"));}catch(Exception ex){}
			try{upload.setComment(rs.getString("comment"));}catch(Exception ex){}
			try{upload.setCategories(rs.getString("categories"));}catch(Exception ex){}
			upload.resetModified();
		}
	}
	public static void fill(ResultSet rs, Collection<Upload> uploadCollection) throws SQLException {
		while(rs.next()){
			Upload upload=new Upload();
			try{upload.setUploadId(rs.getLong("uploadId"));}catch(Exception ex){}
			try{upload.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			try{upload.setName(rs.getString("name"));}catch(Exception ex){}
			try{upload.setOriginFilename(rs.getString("originFilename"));}catch(Exception ex){}
			try{upload.setExtention(rs.getString("extention"));}catch(Exception ex){}
			try{upload.setFiletype(rs.getString("filetype"));}catch(Exception ex){}
			try{upload.setSize(rs.getLong("size"));}catch(Exception ex){}
			try{upload.setUploadDate(rs.getTimestamp("uploadDate"));}catch(Exception ex){}
			try{upload.setDownloads(rs.getLong("downloads"));}catch(Exception ex){}
			try{upload.setLastDownload(rs.getTimestamp("lastDownload"));}catch(Exception ex){}
			try{upload.setPublicPolicy(rs.getInt("publicPolicy"));}catch(Exception ex){}
			try{upload.setPhysicalFilename(rs.getString("physicalFilename"));}catch(Exception ex){}
			try{upload.setPhysicalPath(rs.getString("physicalPath"));}catch(Exception ex){}
			try{upload.setPhysicalAccess(rs.getString("physicalAccess"));}catch(Exception ex){}
			try{upload.setState(rs.getInt("state"));}catch(Exception ex){}
			try{upload.setComment(rs.getString("comment"));}catch(Exception ex){}
			try{upload.setCategories(rs.getString("categories"));}catch(Exception ex){}
			upload.resetModified();
			uploadCollection.add(upload);
		}
	}
	public static boolean update(Upload upload, Connection conn) throws SQLException {
		Vector<String> columns = new Vector<String>();
		for (int i = 0; i < UploadPeer.columnNames.length; i++) {
			if (((UploadBase)upload).modified[i]) {
				columns.add(UploadPeer.columnNames[i]);
			}
		}
		if (columns.isEmpty()) return false;
		StringBuffer sql = new StringBuffer();
		for (String column : columns) {			sql.append(",`"+column+"` = ? ");		}
		sql.deleteCharAt(0);
		sql.insert(0, "UPDATE `Upload` SET ");
		sql.append("WHERE TRUE AND `uploadId` = ? ");
		PreparedStatement pstmt=conn.prepareStatement(sql.toString());
		for (int i=0;i<columns.size();i++) {
			String column = columns.elementAt(i);
			pstmt.setObject(i+1,upload.get(column));
		}
		pstmt.setObject(columns.size()+1+0,upload.get("uploadId"));
		int affected=pstmt.executeUpdate();
		return affected>0;
	}
	public static ResultSet insert(Upload upload, Connection conn) throws SQLException {
		StringBuffer sql1 = new StringBuffer();
		StringBuffer sql2 = new StringBuffer();
		for (String column : columnNames) {
			if (upload.get(column) != null) {
				sql1.append(",`" + column+"`");
				sql2.append(", ? ");
			}
		}
		sql1.deleteCharAt(0);
		sql2.deleteCharAt(0);
		String sql = "INSERT INTO `Upload` (" + sql1.toString() + ") VALUES (" + sql2.toString() + ") ";
		System.out.println(sql);
		PreparedStatement pstmt = conn.prepareStatement(sql);
		for (int i = 0, j = 0; i < columnNames.length; i++) {
			String column = columnNames[i];
			System.out.println(column);
			if (upload.get(column) != null) {
				pstmt.setObject(i + 1 + j, upload.get(column));
			}else{
				j--;
			}
		}
		int affected=pstmt.executeUpdate();
		return pstmt.getGeneratedKeys();
	}
	public static Collection<Upload> getUploadCollection(String conditions, Object[] values, int offset, int limit, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(tableName,columnNames,conditions,values, offset,  limit,conn);
		Collection<Upload> collection=new Vector<Upload>();
		fill(rs,collection);
		return collection;
	}
}
